package com.example.interview.example.bankCallSystem.impl_3;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

//调度器就是dispatcher,它调用Produce不断生成客户,
// 通过WindowList获得一个空闲的窗口,让这个窗口去"叫"自己对应的顾客#
// 例如 一个空闲的vip窗口,就会先去vip队列里找人,如果vip队列里面没有人,窗口就会去quick队列找人
public class Dispatcher {

    private static void run(final Window window, final Consumer consumer, ExecutorService threadPool) {
        threadPool.execute(() -> window.execute(consumer)); //银行窗口办理业务的方法
    }
    // 融合 window + 客户，构成业务办理:
    //quick窗口(获取空闲的window,让当前的customer办理业务，当前[type1的队列]没人就到[type2的队列]找人，以此类推...)
    public void doQuick(ExecutorService threadPool, WindowList Windows, ArrayBlockingQueue<Consumer> vips,
                        ArrayBlockingQueue<Consumer> normals, ArrayBlockingQueue<Consumer> quicks) {
        try {
            while (true) {
                final Window window = Windows.getIdelQuickWindow();//首先获取空闲的quick窗口
                if (window != null) {
                    int flag = 0;// 1:客户已完成办理
                    final Consumer id_quick = quicks.poll(10, TimeUnit.MILLISECONDS);
                    if (id_quick != null){// quick里面有人
                        run(window, id_quick, threadPool);//银行窗口办理业务的方法
                        flag = 1;
                    }
                    if (flag == 0) {
                        final Consumer id_vip = vips.poll(10, TimeUnit.MILLISECONDS);
                        if (id_vip != null) {// quick里面没人 vip队伍里有人排队
                            run(window, id_vip, threadPool);
                            flag = 1;
                        }
                    }
                    if (flag == 0) {
                        final Consumer id_normal = normals.poll(10, TimeUnit.MILLISECONDS);
                        if (id_normal != null) { // quick和vip队伍里没人排队 normal 里面有人
                            run(window, id_normal, threadPool);
                        }
                    }
                } else {
                    Thread.sleep(1000);// Quick窗口正在忙
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //Nomal窗口
    public void doNomal(ExecutorService threadPool, WindowList list, ArrayBlockingQueue<Consumer> vips,
                        ArrayBlockingQueue<Consumer> normals, ArrayBlockingQueue<Consumer> quicks) {
        try {
            while (true) {
                final Window window = list.getIdelNormalWindow();
                if (window != null) {
                    int flag = 0;
                    final Consumer id_normal = normals.poll(10, TimeUnit.MILLISECONDS);
                    if (id_normal != null){// normal 里面有人
                        run(window, id_normal, threadPool);
                        flag = 1;
                    }
                    if (flag == 0) {
                        final Consumer id_quick = quicks.poll(10, TimeUnit.MILLISECONDS);
                        if (id_quick != null){ // normal里面没人 quick里面有人
                            run(window, id_quick, threadPool);
                            flag = 1;
                        }
                    }
                    if (flag == 0) {
                        final Consumer id_vip = vips.poll(10, TimeUnit.MILLISECONDS);
                        if (id_vip != null){// normal里面没人 normal里面没人 vip队伍里有人排队
                            run(window, id_vip, threadPool);
                            flag = 1;
                        }
                    }
                } else {
                    Thread.sleep(1000);// normal窗口正在忙
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //VIP窗口
    public void doVIP(ExecutorService threadPool, WindowList list, ArrayBlockingQueue<Consumer> vips,
                      ArrayBlockingQueue<Consumer> normals, ArrayBlockingQueue<Consumer> quicks) {
        try {
            while (true) {
                final Window window = list.getIdelVIPWindow();
                if (window != null) {
                    // vip窗口空闲
                    // System.out.println("vips is null? "+vips==null);
                    final Consumer id_vip = vips.poll(10, TimeUnit.MILLISECONDS);
                    int flag = 0;
                    if (id_vip != null) {// vip队伍里有人排队
                        run(window, id_vip, threadPool);
                        flag = 1;
                    }
                    if (flag == 0) {
                        final Consumer id_quick = quicks.poll(10, TimeUnit.MILLISECONDS);
                        if (id_quick != null){// vip队伍里没人排队 quick里面有人
                            run(window, id_quick, threadPool);
                            flag = 1;
                        }
                    }
                    if (flag == 0) {
                        final Consumer id_normal = normals.poll(10, TimeUnit.MILLISECONDS);
                        if (id_normal != null){// vip队伍里没人排队 quick也里面有人 normal 里面有人
                            run(window, id_normal, threadPool);
                        }
                    }
                } else {
                    Thread.sleep(1000);// vip窗口正在忙
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
